from django import template
from xadmin.models import Sites, Node, Article, Sites


register = template.Library()


seo_string = '''
     <title>{title}</title>
     <meta content="{keywords}" name="Keywords">
     <meta name="description" content="{content}" />
'''

@register.simple_tag
def get_seo(siteid, nodeid, articleid=0):
    seo_title = ''
    seo_keywords = ''
    seo_des = ''

    if articleid != 0:
        article_obj = Article.objects.get(id=articleid)
        seo_title = article_obj.seo_title
        seo_keywords = article_obj.seo_keyword
        seo_des = article_obj.seo_description
        if seo_title and seo_keywords and seo_des:
            return seo_string.format(title=seo_title, keywords=seo_keywords, content=seo_des)

    node_obj = Node.objects.get(id=nodeid)
    if not seo_title:
        seo_title = node_obj.seo_title
    if not seo_keywords:
        seo_keywords = node_obj.seo_keywords
    if not seo_des:
        seo_des = node_obj.seo_description
    if seo_title and seo_keywords and seo_des:
        return seo_string.format(title=seo_title, keywords=seo_keywords, content=seo_des)

    site_obj = Sites.objects.get(id=siteid)
    if not seo_title:
        seo_title = site_obj.title
    if not seo_keywords:
        seo_keywords = site_obj.keyword
    if not seo_des:
        seo_des = site_obj.des
    seo_html = seo_string.format(title=seo_title, keywords=seo_keywords, content=seo_des)
    #print(seo_html)
    return seo_html